package com.example.vueblog.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vueblog.annotation.Anonymous;
import com.example.vueblog.annotation.Log;
import com.example.vueblog.common.Result;
import com.example.vueblog.common.enums.BusinessTypeEnum;
import com.example.vueblog.common.utils.SecurityUtils;
import com.example.vueblog.common.utils.ServletUtils;
import com.example.vueblog.entity.Blog;
import com.example.vueblog.entity.security.SelfUserDetails;
import com.example.vueblog.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yushunshun
 * @since 2022-05-27
 */
@RestController
@RequestMapping("/blog")
@Api(tags = "博客管理相关接口")
public class BlogController {
    @Autowired
    BlogService blogService;

    @GetMapping("/testLog")
    @Log(title="testLog",businessType = BusinessTypeEnum.OTHER)
    public Result<String> testLog(@RequestParam(value = "name") String name) {
        return Result.ok("请求成功哦..name:"+name);
    }

    @PostMapping("/testLog2")
    @Log(title="testLog2",businessType = BusinessTypeEnum.OTHER)
    public Result<String> testLog2(@RequestBody Map<String,String> map) {
        return Result.ok("请求成功哦2..map:"+ JSON.toJSONString(map));
    }


    /**
     * 分页获取博客数据
     *
     * @param currentPage
     * @return
     */

    @Anonymous
    @GetMapping("/blogs")
    @ApiOperation("博客列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页"),
    })
    public Result<IPage<Blog>> blogs(Integer currentPage) {
        if (currentPage == null || currentPage < 1) currentPage = 1;
        IPage<Blog> page = new Page<>(currentPage, 5);
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<Blog>().orderByDesc("created");
        IPage<Blog> pageData = blogService.page(page, queryWrapper);
        return Result.ok(pageData);
    }

    /**
     * 根据id获取明细数据
     *
     * @param id
     * @return
     */
    @GetMapping("/blog/{id}")
    public Result<Blog> detail(@PathVariable(name = "id") Long id) {
        Blog blog = blogService.getById(id);
        Assert.notNull(blog, "该博客已删除！");
        return Result.ok(blog);
    }

    /**
     * 根据id编辑数据
     * 1.id是空则新增操作
     * 2.如果不是空则做修改操作(非自己博客信息 则无法进行修改)
     *
     * @param blog
     * @return
     */
    @PostMapping("/blog/edit")
    public Result<?> edit(@Validated @RequestBody Blog blog) {
        SelfUserDetails userDetails = SecurityUtils.getUser();
        System.out.println(blog.toString());
        Blog temp = null;
        if (blog.getId() != null) {
            temp = blogService.getById(blog.getId());

            Assert.isTrue(temp.getUserId().equals(Long.valueOf(userDetails.getId().toString())), "没有权限编辑");
        } else {
            temp = new Blog();
            temp.setUserId(Long.valueOf(userDetails.getId().toString()));
            temp.setCreated(LocalDateTime.now());
            temp.setStatus(0);
        }
        BeanUtil.copyProperties(blog, temp, "id", "userId", "created", "status");
        blogService.saveOrUpdate(temp);
        return Result.ok();
    }
}
